﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

intListener MainWindow::experimentStatus;//0 未启动  1 运行  2 实验结束 3 设备故障 4 打开
intListener doorStatus;//0关闭;1开启;
intListener MainWindow::tcpStatus;
intListener MainWindow::speechflagkg;
intListener MainWindow::canRefChart;
intListener MainWindow::experimentStep;
intListener MainWindow::experimentCircle;
intListener MainWindow::startExperiment;
intListener MainWindow::tcpServerStatus;
intListener MainWindow::canAnalyData;
intListener MainWindow::startExperimentStatus;
intListener passwordfl;

StringListener MainWindow::tempDataOne = QString::number(0);
StringListener MainWindow::experimentTime= QString::number(0);

double MainWindow::tempDataTwo =1;
double MainWindow::tempDataThree = 1;
double MainWindow::tempDataFour = 1;
double MainWindow::tempDataFive = 1;
double MainWindow::tempDataSix = 1;
bool MainWindow::reAnalyse=false;
bool MainWindow::isBzsy=false;
int MainWindow::connectType=0;
int MainWindow::tempOrder = 0;
int MainWindow::HoleNum=24;
int MainWindow::oldExperimentStatus=0;
QString outpath="";
int doutfl=0;
int isstopex=0;

QString MainWindow::experimentGuid = "1";
QString MainWindow::experimentSaveDataPath = "";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MainWindow::experimentStatus.set(0);
    MainWindow::startExperiment.set(0);
    MainWindow::tcpServerStatus.set(0);
    MainWindow::speechflagkg.set(2);
    passwordfl.set(0);


    tcpClient=TcpClient::getInstance();
    tcpServer=TcpServer::getInstance();

    spmoduleth = new speechModuleThread();


    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));

    ui->pushButton_7->hide();
    ui->pushButton_8->hide();

#ifdef Q_OS_ANDROID
    ui->returnBtn->setEnabled(false);
#elif defined(Q_OS_WIN)
    ui->returnBtn->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->returnBtn->setEnabled(false);
#endif
    ui->pushButton->hide();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();

    QPalette palette;
    palette.setColor(QPalette::ButtonText, QColor(255,255,255));
    ui->pushButton_2->setText("新建实验");
    ui->pushButton_2->setPalette(palette);
    MainWindow::tcpStatus.set(1);
    // 创建一个QWidget作为容器
    container = new QWidget;
    container->setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    container->setWindowTitle("提示");
    container->setStyleSheet("background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #007bd8,stop:0.5 #474ac1 ,stop:1 #001a5a)");
    label=new QLabel();
    label->setMinimumSize(239,303);
    label->setWindowFlags(Qt::WindowStaysOnTopHint); // 设置为置顶窗口
    label->setAlignment(Qt::AlignCenter); // 设置文本居中
    labelw=new QLabel();
    labelw->setMinimumSize(300,40);
    labelw->setMaximumSize(300,40);
    labelw->setStyleSheet("color: rgb(255, 255, 255);");
    labelw->setWindowFlags(Qt::WindowStaysOnTopHint); // 设置为置顶窗口
    labelw->setAlignment(Qt::AlignCenter); // 设置文本居中
    // 创建一个垂直布局
    QVBoxLayout *layout = new QVBoxLayout(container);

    // 将QLabel和QPushButton添加到布局中
    layout->addWidget(label); // label将占据大部分空间
    layout->addWidget(labelw); // button将显示在label下方

    QObject::connect(&experimentStatus, &intListener::valueChanged, this,&MainWindow::experimentStatusChanged,Qt::QueuedConnection);
    QObject::connect(&speechflagkg, &intListener::valueChanged,this,&MainWindow::speechflagkgChanged,Qt::QueuedConnection);
    QObject::connect(&tcpStatus, &intListener::valueChanged, this,&MainWindow::tcpStatusChanged);
    QObject::connect(&tcpServerStatus, &intListener::valueChanged, this,&MainWindow::tcpServerChanged);
    QObject::connect(&canAnalyData, &intListener::valueChanged, this,&MainWindow::dataAnalyse);
    QObject::connect(&passwordfl, &intListener::valueChanged, this,&MainWindow::passwordchange);

    this->hms.hour=0;
    this->hms.minute = 0;
    this->hms.second = 0;
    m_timer = new QTimer(this);
    m_timer->stop();    // 默认定时器关闭
    m_timer->setSingleShot(false);
    m_timer->setTimerType(Qt::PreciseTimer);

    // 间隔时间 1s
    m_timer->setInterval(1000);
    connect(m_timer, &QTimer::timeout, this, &MainWindow::do_timer_timeout);

    labelTimer = new QTimer(this);
    labelTimer->stop();    // 默认定时器关闭
    labelTimer->setSingleShot(false);
    labelTimer->setTimerType(Qt::PreciseTimer);
    labelTimer->setInterval(5000);
    connect(labelTimer, &QTimer::timeout, this, &MainWindow::closeMovieLabel);

    runTimer = new QTimer(this);
    runTimer->start();    // 默认定时器关闭
    runTimer->setSingleShot(false);
    runTimer->setTimerType(Qt::PreciseTimer);
    runTimer->setInterval(60000);
    connect(runTimer, &QTimer::timeout, this, &MainWindow::recordRunTime);

    expTimer = new QTimer(this);
    expTimer->stop();    // 默认定时器关闭
    expTimer->setSingleShot(false);
    expTimer->setTimerType(Qt::PreciseTimer);
    expTimer->setInterval(1000);
    connect(expTimer, &QTimer::timeout, this, &MainWindow::tempTimerUpdate);

    mainMovie = new QMovie(":/pic/picture/gif1.gif");
    ui->label_5->setMovie(mainMovie);
    mainMovie->start();
    tempMovie = new QMovie();
    ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->pushButton_9->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showEvent(QShowEvent *event)
{
    if(MainWindow::tcpServerStatus.get()==1){
        ui->label_6->show();
        ui->pushButton_12->show();
    }else {
        ui->label_6->hide();
        ui->pushButton_12->hide();
    }
}

void MainWindow::experimentStatusChanged(){
    if(MainWindow::tcpServerStatus.get()==0){
        if(MainWindow::experimentStatus.get()==0){
            ui->pushButton_2->setText("新建实验");
            ui->label_10->setText("CardType-Chip Analysis Software V1.8");
            ui->label_4->hide();
            ui->pushButton->hide();
            ui->pushButton_10->hide();
            ui->pushButton_11->hide();

            ui->pushButton_2->setEnabled(true);

#ifdef Q_OS_ANDROID
            ui->returnBtn->setEnabled(false);
#elif defined(Q_OS_WIN)
            ui->returnBtn->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
            ui->returnBtn->setEnabled(false);
#endif

            ui->pushButton_3->setEnabled(true);
            ui->pushButton_3->setStyleSheet("border-image: url(:/pic/picture/m1.png);color: rgb(255, 255, 255);");

            ui->pushButton_4->setEnabled(true);
            ui->pushButton_4->setStyleSheet("border-image: url(:/pic/picture/m2.png);color: rgb(255, 255, 255);");

            ui->pushButton_5->setEnabled(true);
            ui->pushButton_5->setStyleSheet("border-image: url(:/pic/picture/m3.png);color: rgb(255, 255, 255);");

            ui->pushButton_6->setEnabled(true);
            ui->pushButton_6->setStyleSheet("border-image: url(:/pic/picture/m4.png);color: rgb(255, 255, 255);");

            ui->pushButton_7->hide();
            ui->pushButton_8->hide();


            if(mainMovie){
                mainMovie->stop();
                delete mainMovie;
                mainMovie=0;
            }
            mainMovie = new QMovie(":/pic/picture/gif1.gif");
            ui->label_5->setMovie(mainMovie);
            mainMovie->start();

        }
        if(MainWindow::experimentStatus.get()==1){
            if(mainMovie){
                mainMovie->stop();
                delete mainMovie;
                mainMovie=0;
            }
            mainMovie = new QMovie(":/pic/picture/gif2.gif");
            ui->label_5->setMovie(mainMovie);
            mainMovie->start();

            ui->pushButton_7->show();
            ui->pushButton_8->show();
            ui->label_4->show();
            ui->pushButton_3->setEnabled(false);
            ui->pushButton_3->setStyleSheet("border-image: url(:/pic/picture/jy1.png);color: rgb(255, 255, 255);");

            ui->pushButton_4->setEnabled(false);
            ui->pushButton_4->setStyleSheet("border-image: url(:/pic/picture/jy2.png);color: rgb(255, 255, 255);");

            ui->pushButton_5->setEnabled(false);
            ui->pushButton_5->setStyleSheet("border-image: url(:/pic/picture/jy3.png);color: rgb(255, 255, 255);");

            ui->pushButton_6->setEnabled(false);
            ui->pushButton_6->setStyleSheet("border-image: url(:/pic/picture/jy4.png);color: rgb(255, 255, 255);");

            QString duration;
            QString id="";

            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString sql=QString("Select ID,Duration From AmplProgram where id=(Select AmplGuid from ExperimentInfo where id='%1')").arg(experimentGuid);
            query.exec(sql);
            while (query.next()) {
                duration = query.value("Duration").toString();
                id=query.value("Id").toString();
            }

            sql=QString("Select CycleNum from AmplStep where ProgramId='%1' and "
                        "CASE WHEN "
                        "(Select count(*) from AmplStep where ProgramId='%1' and IsCollect='是')>0 "
                        "then "
                        "IsCollect='是' "
                        "else "
                        "StepNum=(Select max(StepNum) from AmplStep where programId='%1') "
                        "end").arg(id);
            query.exec(sql);
            while (query.next()) {
                CycleNum = query.value("CycleNum").toString();
            }

            ui->pushButton->show();
            ui->pushButton_10->show();
            ui->pushButton_11->show();

            sql="Select ExperimentName from ExperimentInfo";
            query.exec(sql);
            while (query.next()) {
                QString fileName=query.value("ExperimentName").toString();
                ui->label_10->setText(fileName);
            }

            QPalette palette;
            palette.setColor(QPalette::ButtonText, QColor(255,255,255));

            ui->pushButton_2->setPalette(palette);
            ui->pushButton_2->setText("00:00");
            ui->pushButton_2->setEnabled(false);

            ui->pushButton_11->setPalette(palette);
            ui->pushButton_11->setText(QString ("0/%1").arg(CycleNum));

            palette.setColor(QPalette::ButtonText, QColor(144,251,255));
            ui->pushButton->setPalette(palette);
            ui->pushButton->setText("正在运行");


            palette.setColor(QPalette::ButtonText, QColor(156,186,255));
            ui->pushButton_10->setPalette(palette);
            ui->pushButton_10->setText("0°C");

            double hour=0;
            double min=0;
            double second=0;

            if(duration.contains("时")){
                QStringList hourList=  duration.split("时");
                hour=hourList[0].toDouble();
                duration.remove(0,hourList[0].length());
                duration.remove("时");
            }
            if(duration.contains("分")){
                QStringList hourList=  duration.split("分");
                min=hourList[0].toDouble();
                duration.remove(0,hourList[0].length());
                duration.remove("分");
            }
            if(duration.contains("秒")){
                QStringList hourList=  duration.split("秒");
                second=hourList[0].toDouble();
            }

            this->hms.hour = hour;
            this->hms.minute = min;
            this->hms.second = second;
            m_timer->start();

            expHms.hour=0;
            expHms.minute=0;
            expHms.second=0;
            expTimer->start();
        }
        if(MainWindow::experimentStatus.get()==2){
            if(mainMovie){
                mainMovie->stop();
                delete mainMovie;
                mainMovie=0;
            }
            mainMovie = new QMovie(":/pic/picture/gif3.gif");
            ui->label_5->setMovie(mainMovie);
            mainMovie->start();

            ui->pushButton_7->hide();
            ui->pushButton_8->hide();
            ui->label_4->hide();

            ui->pushButton->show();
            ui->pushButton_10->hide();
            ui->pushButton_11->hide();

            ui->returnBtn->setStyleSheet("border-image: url(:/pic/picture/t9.png);"
                                         "background-color: rgba(0, 0, 0, 0.3);");
            ui->returnBtn->setEnabled(true);

            ui->pushButton->setText("已完成");
            ui->pushButton_2->setText("导出报告");
            ui->pushButton_2->setEnabled(true);

            QPalette palette;
            palette.setColor(QPalette::ButtonText, QColor(255,255,255)); // 将字体颜色设置为红色
            ui->pushButton_2->setPalette(palette);
            ui->pushButton_11->setPalette(palette);

            palette.setColor(QPalette::ButtonText, QColor(255,188,13)); // 将字体颜色设置为红色
            ui->pushButton->setPalette(palette);

            palette.setColor(QPalette::ButtonText, QColor(156,186,255)); // 将字体颜色设置为红色
            ui->pushButton_10->setPalette(palette);

            eqptinfo eqInfo;
            QString operh= eqInfo.operhread();
            QString durah=eqInfo.durahread();
            QString times=eqInfo.timesread();

            eqInfo.timeswrite(QString::number(times.toDouble()+1));


            QList<QString> dateTime= durah.split(':');

            dateTime[0]=QString::number(dateTime[0].toDouble() +expHms.hour);
            dateTime[1]=QString::number(dateTime[1].toDouble() +expHms.minute);
            dateTime[2]=QString::number(dateTime[2].toDouble() +expHms.second);


            if(dateTime[2].toInt()>=60 ){
                dateTime[1]=QString::number(dateTime[1].toInt() + 1);
                dateTime[2]=QString::number(dateTime[2].toInt()-60);
            }

            if(dateTime[1].toInt()>=60){
                dateTime[0]=QString::number(dateTime[0].toInt() + 1);
                dateTime[1]=QString::number(dateTime[1].toInt()-60);
            }

            eqInfo.durahwrite(dateTime[0]+":" + dateTime[1]+":" + dateTime[2]);

            if(isstopex==0){
                derivedata1 = new derivedata();
                derivedata1->refOriginalCurveTable();
                derivedata1->refAmplificationCurveTable();
                derivedata1->standardCurveChart();
                derivedata1->guide();
                derivedata1->datao();
            }else{
                isstopex=0;
                return;
            }


        }
        if(MainWindow::experimentStatus.get()==3){
            if(mainMovie){
                mainMovie->stop();
                delete mainMovie;
                mainMovie=0;
            }
            mainMovie = new QMovie(":/pic/picture/gif4.gif");
            ui->label_5->setMovie(mainMovie);
            mainMovie->start();
            ui->label_4->hide();


            ui->pushButton->show();
            ui->pushButton_10->hide();
            ui->pushButton_11->hide();
            ui->pushButton->setText("故障");
            ui->pushButton_2->setText("设备异常");
            ui->pushButton_2->setEnabled(true);

            ui->returnBtn->setStyleSheet("border-image: url(:/pic/picture/t9.png);"
                                         "background-color: rgba(0, 0, 0, 0.3);");
            ui->returnBtn->setEnabled(true);

            QPalette palette;
            palette.setColor(QPalette::ButtonText, QColor(255,255,255)); // 将字体颜色设置为红色
            ui->pushButton_2->setPalette(palette);

            palette.setColor(QPalette::ButtonText, QColor(255,13,13)); // 将字体颜色设置为红色
            ui->pushButton->setPalette(palette);

            ui->pushButton_3->setEnabled(true);
            ui->pushButton_3->setStyleSheet("border-image: url(:/pic/picture/m1.png);color: rgb(255, 255, 255);");

            ui->pushButton_4->setEnabled(true);
            ui->pushButton_4->setStyleSheet("border-image: url(:/pic/picture/m2.png);color: rgb(255, 255, 255);");

            ui->pushButton_5->setEnabled(true);
            ui->pushButton_5->setStyleSheet("border-image: url(:/pic/picture/m3.png);color: rgb(255, 255, 255);");

            ui->pushButton_6->setEnabled(true);
            ui->pushButton_6->setStyleSheet("border-image: url(:/pic/picture/m4.png);color: rgb(255, 255, 255);");

            ui->pushButton_7->hide();
            ui->pushButton_8->hide();
        }
    }
}

void MainWindow::tcpStatusChanged(){
    if(MainWindow::tcpStatus.get()==1){
        if(MainWindow::experimentStatus.get()==1){
            MainWindow::experimentStatus.set(3);
        }
        ui->label_2->setStyleSheet("border-image: url(:/pic/picture/tx1.png);");
        ui->label_3->setText("未连接");
    }else {
        if(MainWindow::experimentStatus.get()==1){
            MainWindow::experimentStatus.set(0);
        }
        ui->label_2->setStyleSheet("border-image: url(:/pic/picture/tx2.png);");
        ui->label_3->setText("已连接");
    }
}

void MainWindow::tcpServerChanged(){
    if(MainWindow::tcpServerStatus.get()==1){
        if(mainMovie){
            mainMovie->stop();
            delete mainMovie;
            mainMovie=0;
        }

        ui->pushButton->hide();
        ui->pushButton_10->hide();
        ui->pushButton_11->hide();
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/t5.png);");
        ui->pushButton_2->setText("联机中");
        ui->pushButton_3->hide();
        ui->pushButton_4->hide();
        ui->pushButton_5->hide();
        ui->pushButton_6->hide();
        ui->pushButton_12->show();
        ui->pushButton_7->hide();
        ui->pushButton_8->hide();
        ui->label_6->show();
        ui->label->hide();
        ui->label_2->hide();
        ui->label_3->hide();
        ui->pushButton_2->setEnabled(false);
        ui->pushButton_9->setEnabled(false);
    }
    else
    {
        ui->label->show();
        ui->label_2->show();
        ui->label_3->show();
        ui->pushButton_3->show();
        ui->pushButton_4->show();
        ui->pushButton_5->show();
        ui->pushButton_6->show();
        ui->pushButton_12->hide();
        ui->label_6->hide();

        experimentStatusChanged();
    }
}

void MainWindow::dataAnalyse(){
    if(MainWindow::canAnalyData.get()==0)
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        bool canAnaly=false;
        QString sql=QString("Select Count(*) as Num From AmplStep where ProgramId=(Select AmplGuid from ExperimentInfo where id='%1') and IsCollect='是'").arg(experimentGuid);
        query.exec(sql);
        while(query.next()){
            canAnaly=query.value("Num").toInt()>0;
        }
        if(canAnaly){
            //            dataAnalyThread = new DataAnalysisThread();
            //            dataAnalyThread->run();
            dataAnalyJ=new DataAnalysisByJThread();
            dataAnalyJ->run(true);
        }
    }
}

void MainWindow::passwordchange()
{
    if(passwordfl.get()==1){
        this->hide();
        passwordfl.set(0);
    }
}

void MainWindow::speechflagkgChanged(){
    spmoduleth->run();
}

void MainWindow::on_pushButton_6_clicked()
{
    this->hide();
    emit changeSetUpWindow();
}

void MainWindow::on_pushButton_4_clicked()
{
    this->hide();
    emit changeAmplWindow();
}

void MainWindow::on_pushButton_3_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(nullptr, QObject::tr("Open File"),QDir::homePath(), QObject::tr("All Files (*)"));
    if(!filePath.isNull()){
        MainWindow::experimentSaveDataPath=filePath;
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        MainWindow::experimentStatus.set(4);
        db.transaction();

        QString sql="Delete from SampleInfo";
        query.exec(sql);

        sql="Delete From AmplProgramTemp";
        query.exec(sql);

        sql="Delete From AmplStepTemp";
        query.exec(sql);

        sql="Delete From SampleAccessInfo";
        query.exec(sql);

        sql="Delete From ExperimentInfo";
        query.exec(sql);

        sql="Delete From FluorescenceData";
        query.exec(sql);

        sql="Delete From FluorescenceLvData";
        query.exec(sql);

        sql="Delete From FluorescenceNormalData";
        query.exec(sql);

        sql="Delete From FluorescenceResultTable";
        query.exec(sql);

        QFile file(filePath);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream txtInput(&file);
        int num=0;
        bool isOpen=true;
        bool refPos=false;

        while (!txtInput.atEnd())
        {
            QString lineStr = txtInput.readLine();
            if(num==0 &&lineStr=="96Types"){
                tcts *tctsWindow = new tcts();
                tctsWindow->infochange("提示","info","您打开的数据文件与当前软件版本不兼容，请重新选择",true,true);
                tctsWindow->show();
                isOpen=false;
                return;
            }
            else if(num==0 &&lineStr=="cardType"){
                num++;
            }
            else
            {
                num++;
                if(lineStr.contains("Insert into AmplProgram values"))
                {
                    lineStr.replace("Insert into AmplProgram values","Insert into AmplProgramTemp values");
                }
                if(lineStr.contains("Insert into AmplStep values")){
                    lineStr.replace("Insert into AmplStep values","Insert into AmplStepTemp values");
                }
                if(lineStr.contains("Insert into SampleInfo values")){
                    if(lineStr.split(",").length()==7){
                        lineStr.replace("Insert into SampleInfo values","Insert into SampleInfo( ID,ExperimentGuid,ColNum,RowNum,SampleName,SampleType,SampleConc) values");
                        refPos=true;
                    }
                }
                query.exec(lineStr);
            }
        }
        if(refPos){
            sql="Update SampleInfo Set Pos=RowNum*4+1+ColNum";
            query.exec(sql);
        }

        db.commit();
        if(isOpen){
            sql="Update SampleInfo set Pos=RowNum*4+ColNum+1";
            query.exec(sql);

            sql="Select count(*) Sum from FluorescenceData;";

            query.exec(sql);
            bool dataStatus=false;
            while (query.next()) {
                dataStatus=query.value("Sum").toInt()==0;
            }

            if(dataStatus){
                MainWindow::experimentStatus.set(5);
                sql="Update ExperimentInfo Set AmplGuid=''";
                query.exec(sql);
                this->hide();
                emit changeSampleWindow();
            }else {
                MainWindow::experimentStatus.set(4);
                MainWindow::reAnalyse=true;
                this->hide();
                emit changeAnalyWindow();
            }
        }
    }
}

void MainWindow::recordRunTime(){
    eqptinfo eqInfo;
    QString oldTime = eqInfo.operhread();
    QList<QString> time= oldTime.split(':');
    double hour=time[0].toDouble();
    double minute=time[1].toDouble();

    if(minute+1>=60){
        hour+=1;
        minute=0;
    }else{
        minute+=1;
    }
    eqInfo.operhwrite(QString::number(hour)+":"+QString::number(minute));
}

void MainWindow::do_timer_timeout()
{
    if(MainWindow::experimentStatus.get()==2|| MainWindow::experimentStatus.get()==3){
        m_timer->stop();
        return;
    }
    bool retBool = this->subtractOneSecond();


    if(retBool){
        QString hour=QString::number(hms.hour).length()< 2?"0"+QString::number(hms.hour):QString::number(hms.hour);
        QString minute=QString::number(hms.minute).length()<2?"0"+QString::number(hms.minute):QString::number(hms.minute);
        QString second=QString::number(hms.second).length()<2?"0"+QString::number(hms.second):QString::number(hms.second);

        ui->pushButton_2->setText(hour+":"+ minute+":"+second);
    }
    QString temp=common->SixteenToTen(MainWindow::tempDataOne.toString());
    double newTemp=temp.toDouble()/100;

    QString oldTempString=ui->pushButton_10->text();
    QList<QString> oldTempList=oldTempString.split("°");
    double oldTemp=oldTempList[0].toDouble();

    if(newTemp>oldTemp){
        if(tempMovie){
            tempMovie->stop();
            delete tempMovie;
            tempMovie=0;
        }
        tempMovie= new QMovie(":/pic/picture/sw.gif");
        ui->label_4->setMovie(tempMovie);
        tempMovie->start();
    }
    else if(newTemp<oldTemp)
    {
        if(tempMovie){
            tempMovie->stop();
            delete tempMovie;
            tempMovie=0;
        }
        tempMovie = new QMovie(":/pic/picture/jw.gif");
        ui->label_4->setMovie(tempMovie);
        tempMovie->start();
    }

    ui->pushButton_10->setText(QString::number(temp.toDouble()/100,'f',2)+"°C");
    ui->pushButton_11->setText(QString("%1/%2").arg(QString::number(MainWindow::experimentCircle.get()),CycleNum));
}

void MainWindow::tempTimerUpdate(void){
    if(MainWindow::experimentStatus.get()==2 || MainWindow::experimentStatus.get()==3){
        expTimer->stop();
        return;
    }
    expHms.second++;
    if(expHms.second==60){
        expHms.minute++;
        expHms.second=0;
        if(expHms.minute==60){
            expHms.hour++;
            expHms.minute=0;
        }
    }
    MainWindow::experimentTime=QString("%1时 %2分 %3秒").arg(QString::number(expHms.hour),QString::number(expHms.minute),QString::number(expHms.second));
}

// 减一秒
bool MainWindow::subtractOneSecond()
{
    // 若全部等于0，则返回false
    if(hms.second <= 0 && hms.minute == 0&& hms.hour==0){
        return false;
    }
    // 秒钟减1
    hms.second--;
    if(hms.second < 0){
        hms.minute--;
        hms.second = 59;
        if(hms.minute<0 && hms.hour>0){
            hms.hour--;
            hms.minute=59;
        }
    }
    return true;
}

void MainWindow::on_pushButton_8_clicked()
{
    this->hide();
    emit changeSampleWindow();
}

void MainWindow::on_pushButton_7_clicked()
{
    this->hide();
    emit changeExpeWindow();
}

void MainWindow::on_returnBtn_clicked()
{
    MainWindow::experimentStatus.set(0);
}

void MainWindow::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
    }
    else
    {
        doorStatus.set(0);
    }
}


void MainWindow::closeMovieLabel(){
    container->close();
    labelTimer->stop();
}

void MainWindow::on_pushButton_5_clicked()
{
    //    this->hide();
    emit changeMaintainWindow();
}

void MainWindow::on_pushButton_clicked()
{
    if(MainWindow::experimentStatus.get()==2){
        this->hide();
        emit changeAnalyWindow();
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    if(MainWindow::experimentStatus.get()==0){
        if(MainWindow::tcpStatus.get()==1){

            tcts *tctsWindow = new tcts();
            tctsWindow->infochange("提示","info","未连接设备,请先连接设备",true,true);
            tctsWindow->show();

            connect(tctsWindow->ui->pushButton_2, &QPushButton::clicked,[this](){

                this->hide();
                emit changeSetUpWindow();
            });

        }else{
            MainWindow::experimentStatus.set(0);
            this->hide();
            emit changeExpInfoWindow();
        }
    }
    else if(MainWindow::experimentStatus.get()==4){
        if(MainWindow::tcpStatus.get()==1){
            tcts *tctsWindow = new tcts();
            tctsWindow->infochange("提示","info","未连接设备,请先连接设备",true,true);
            tctsWindow->show();

            connect(tctsWindow->ui->pushButton_2, &QPushButton::clicked,[this](){
                this->hide();
                emit changeSetUpWindow();
            });
        }else{
            MainWindow::experimentStatus.set(0);
            this->hide();
            emit changeExpInfoWindow();
        }
    }
    else if(MainWindow::experimentStatus.get()==3){
        this->hide();
        emit changeErrorWindow();
    }else if(MainWindow::experimentStatus.get()==2){
        doutfl=1;
        // 获取用户选择的文件夹路径
        outpath = QFileDialog::getExistingDirectory(this,
                                                    tr("选择文件夹"),
                                                    QDir::currentPath(), // 初始目录
                                                    QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        if(outpath.isEmpty()){
            return;
        }

        derivedata1 = new derivedata();
        derivedata1->datao();
        doutfl=0;
    }
}

void MainWindow::on_pushButton_10_clicked()
{

    if(MainWindow::experimentStatus.get()==1 &&MainWindow::HoleNum==24){
        this->hide();
        emit changeExpTempWindow();
    }
}

void MainWindow::on_pushButton_12_clicked()
{
    tcpServer->closeConnect();
    MainWindow::tcpServerStatus.set(0);
}
